package com.lemon.smartsafetynew.ui.adapter.user;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lemon.smartsafety.R;
import com.lemon.smartsafetynew.entity.user.LoginNameVo;

import java.util.List;

/**
 * Created by JiangAn on 2018/4/10.
 */

public class LoginNameAdapter extends BaseAdapter {
    private List<LoginNameVo> mList;
    private LayoutInflater inflater;
    private DeleteListener mDeleteListener;


    public LoginNameAdapter(Context context, List<LoginNameVo> mList, DeleteListener deleteListener) {
        this.mList = mList;
        inflater = LayoutInflater.from(context);
        this.mDeleteListener = deleteListener;
    }

    @Override
    public int getCount() {
        return mList == null ? 0 : mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        ViewHolder holder = null;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_loginname_histroy, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.tvLoginName.setText(mList.get(position).getLoginName());
        holder.delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDeleteListener.delete(position);
            }
        });

        return convertView;
    }

    public class ViewHolder {
        private TextView tvLoginName;
        private ImageView delete;
        private LinearLayout layout;

        public ViewHolder(View convertView) {
            tvLoginName = (TextView) convertView.findViewById(R.id.company_name_tv);
            layout = (LinearLayout) convertView.findViewById(R.id.layout);
            delete = (ImageView) convertView.findViewById(R.id.del_iv);
        }
    }

    public interface DeleteListener {
        void delete(int position);
    }
}
